<template>
  <div class="newly">
    <div class="cart">
      <el-card class="boxcard">
        <div slot="header" class="clearfix">
          <span>新增文章</span>
        </div>
        <div>
          <el-form
            ref="ruleForm"
            size="mini"
            :model="ruleForm"
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="作者" prop="author">
                  <el-input v-model="ruleForm.author" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="标题" prop="title">
                  <el-input v-model="ruleForm.title" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="是否显示">
                  <el-switch
                    v-model="ruleForm.isShow"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-col :span="18">
              <el-form-item label="摘要" prop="desc">
                <el-input v-model="ruleForm.desc" type="textarea" />
              </el-form-item>
            </el-col>
            <el-col :span="18">
              <el-form-item label="图片上传">
                <el-upload
                  ref="upremove"
                  class="upload-demo"
                  :on-success="handImg"
                  :headers="tokens"
                  action="/lejuAdmin/material/uploadFileOss"
                >
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>

              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="切换文本类型">
                <el-radio-group v-model="radio" @change="sisui(radio)">
                  <el-radio :label="1">富文本</el-radio>
                  <el-radio :label="2">MarkdownEd</el-radio>
                </el-radio-group></el-form-item>
            </el-col>
            <!-- 富文本 区域 -->
            <el-col :span="22">
              <el-form-item label="文本区域">
                <tinymce v-if="sho" v-model="ruleForm.content1" />
                <MarkdownEditor v-if="!sho" ref="Markdown" v-model="tent" />
              </el-form-item>
            </el-col>
            <!-- MarkdownEd 区域
            <el-col :span="22">
              <el-form-item label="文本区域">

              </el-form-item>
            </el-col> -->
            <!-- 确认创建按钮 -->
            <el-col :span="22" :offset="6">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="submitForm"
                >立即创建</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import MarkdownEditor from '@/components/MarkdownEditor/index.vue'
import tinymce from '@/components/Tinymce'
import { addArticle } from '@/api/Management/article/index.js'
import { getToken } from '@/utils/myAuth'
export default {
  components: {
    tinymce,
    MarkdownEditor
  },
  data() {
    return {
      sho: true,
      radio: 1,
      tent: '',
      ruleForm: {
        author: '',
        collectCount: '',
        content1: '',
        content2: '',
        coverImg: '',
        createTime: '',
        editorType: '',
        id: '',
        isShow: '',
        modifyTime: '',
        summary: '',
        title: '',
        viewCount: '',
        zanCount: ''
      },
      rules: {
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          {
            min: 1,
            max: 20,
            message: '长度在 31 到 20 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    // 上传图片没有携带本地存储的登录信息，需要手动添加上去
    tokens() {
      return {
        token: getToken()
      }
    }
  },
  methods: {
    // 图片上传成功
    handImg(response, file, fileList) {
      // 上传成功就把图片地址 赋值 给接口需要的数据
      this.ruleForm.coverImg = response.data.fileUrl
      console.log(this.ruleForm.coverImg)
      // 上传成功就清空上传列表
      this.$refs.upremove.clearFiles()
    },
    // 文本类型切换
    sisui(e) {
      if (e === 1) {
        console.log(1)
        this.sho = !this.sho
      } else if (e === 2) {
        console.log(2)
        this.sho = !this.sho
      }
    },
    // 点击立即创建
    submitForm() {
      console.log()
      console.log(this.$refs.Markdown.getValue(), this.$refs.Markdown.getHtml())
      this.ruleForm.content1 = this.$refs.Markdown.getValue()
      this.ruleForm.content2 = this.$refs.Markdown.getHtml()
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          alert('submit!')
          addArticle(this.ruleForm).then(res => {
            // console.log(res)
            // console.log(res.success)
            if (res.success) {
              this.$message({
                message: '新建上传成功',
                type: 'success'
              })
            } else {
              this.$message.error('新建上传失败')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.newly {

  .cart {
    margin: 30px;
    .boxcard {
      .clearfix {
        font-size: 20px;
      }
    }
  }
}
</style>
